home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / os2 / mpegenc / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  11.9 KB  |  443 lines

  1. /*===========================================================================*
  2.  * main.c                                     *
  3.  *                                         *
  4.  *    Main procedure                                 *
  5.  *                                         *
  6.  * EXPORTED PROCEDURES:                                 *
  7.  *    main                                     *
  8.  *                                         *
  9.  *===========================================================================*/
  10.  
  11. /*
  12.  * Copyright (c) 1993 The Regents of the University of California.
  13.  * All rights reserved.
  14.  *
  15.  * Permission to use, copy, modify, and distribute this software and its
  16.  * documentation for any purpose, without fee, and without written agreement is
  17.  * hereby granted, provided that the above copyright notice and the following
  18.  * two paragraphs appear in all copies of this software.
  19.  *
  20.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  21.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  22.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  23.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  *
  25.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  26.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  28.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  29.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  30.  */
  31.  
  32. /*  
  33.  *  $Header: /n/picasso/users/keving/encode/src/RCS/main.c,v 1.3 1993/07/22 22:23:43 keving Exp keving $
  34.  *  $Log: main.c,v $
  35.  * Revision 1.3  1993/07/22  22:23:43  keving
  36.  * nothing
  37.  *
  38.  * Revision 1.2  1993/06/30  20:06:09  keving
  39.  * nothing
  40.  *
  41.  * Revision 1.1  1993/02/17  23:18:20  dwallach
  42.  * Initial revision
  43.  *
  44.  */
  45.  
  46.  
  47. /*==============*
  48.  * HEADER FILES *
  49.  *==============*/
  50.  
  51. #include "all.h"
  52. #include "mtypes.h"
  53. #include "mpeg.h"
  54. #include "search.h"
  55. #ifdef OS2
  56. /* @@@ FAT 8.3 convention */
  57. #include "prototyp.h"
  58. #else
  59. #include "prototypes.h"
  60. #endif
  61. #include "param.h"
  62. #include "parallel.h"
  63. #ifdef OS2
  64. /* @@@ FAT 8.3 convention */
  65. #include "readfram.h"
  66. #else
  67. #include "readframe.h"
  68. #endif
  69. #include "combine.h"
  70. #include "frames.h"
  71.  
  72.  
  73. int    main _ANSI_ARGS_((int argc, char **argv));
  74.  
  75. /*==================*
  76.  * STATIC VARIABLES *
  77.  *==================*/
  78.  
  79. static int    frameStart = -1;
  80. static int    frameEnd;
  81.  
  82.  
  83. /*==================*
  84.  * GLOBAL VARIABLES *
  85.  *==================*/
  86.  
  87. extern time_t IOtime;
  88. int    whichGOP = -1;
  89. boolean    childProcess = FALSE;
  90. boolean    ioServer = FALSE;
  91. boolean    outputServer = FALSE;
  92. boolean    decodeServer = FALSE;
  93. int    quietTime = 0;
  94. boolean    frameSummary = TRUE;
  95. boolean debugSockets = FALSE;
  96. boolean debugMachines = FALSE;
  97.  
  98.  
  99. /*===============================*
  100.  * INTERNAL PROCEDURE prototypes *
  101.  *===============================*/
  102.  
  103. static void Usage _ANSI_ARGS_((void));
  104.  
  105.  
  106. /*=====================*
  107.  * EXPORTED PROCEDURES *
  108.  *=====================*/
  109.  
  110.  
  111. /*===========================================================================*
  112.  *
  113.  * main
  114.  *
  115.  *    see man page.  run without arguments to see usage
  116.  *
  117.  * RETURNS:    0 if all is well; 1 on most if not all errors
  118.  *
  119.  *===========================================================================*/
  120. int
  121. main(argc, argv)
  122.     int argc;
  123.     char **argv;
  124. {
  125.     FILE *ofp = NULL;
  126.     register int index;
  127.     int        function = ENCODE_FRAMES;
  128.     int        portNumber = 0;
  129.     char    *hostName = NULL;
  130.     int32   totalTime = -1;
  131.     int        maxMachines = 0x7fffffff;
  132.     int        outputFrames = 0;
  133.     time_t  initTimeStart, initTimeEnd;
  134.     time_t  framesTimeStart, framesTimeEnd;
  135.  
  136.     time(&initTimeStart);
  137.  
  138.     if ( argc == 1 ) {
  139.     Usage();
  140.     }
  141.  
  142.     SetStatFileName("");
  143.  
  144.     /* parse the arguments */
  145.     index = 1;
  146.     while ( index < argc-1 ) {
  147.     if ( argv[index][0] != '-' ) {
  148.         Usage();
  149.     }
  150.  
  151.     if ( strcmp(argv[index], "-stat") == 0 ) {
  152.         if ( index+1 < argc-1 ) {
  153.         SetStatFileName(argv[index+1]);
  154.         index += 2;
  155.         } else {
  156.         Usage();
  157.         }
  158.     } else if ( strcmp(argv[index], "-gop") == 0 ) {
  159.         if ( (function != ENCODE_FRAMES) || (frameStart != -1) ) {
  160.         Usage();
  161.         }
  162.  
  163.         if ( index+1 < argc-1 ) {
  164.         whichGOP = atoi(argv[index+1]);
  165.         index += 2;
  166.         } else {
  167.         Usage();
  168.         }
  169.     } else if ( strcmp(argv[index], "-frames") == 0 ) {
  170.         if ( (function != ENCODE_FRAMES) || (whichGOP != -1) ) {
  171.         Usage();
  172.         }
  173.  
  174.         if ( index+2 < argc-1 ) {
  175.         frameStart = atoi(argv[index+1]);
  176.         frameEnd = atoi(argv[index+2]);
  177.  
  178.         if ( (frameStart > frameEnd) || (frameStart < 0) ) {
  179.             fprintf(stderr, "ERROR:  bad frame numbers!\n");
  180.             Usage();
  181.         }
  182.  
  183.         index += 3;
  184.         } else {
  185.         Usage();
  186.         }
  187.     } else if ( strcmp(argv[index], "-combine_gops") == 0 ) {
  188.         if ( (function != ENCODE_FRAMES) || (whichGOP != -1) || 
  189.          (frameStart != -1) ) {
  190.         Usage();
  191.         }
  192.  
  193.         function = COMBINE_GOPS;
  194.         index++;
  195.     } else if ( strcmp(argv[index], "-combine_frames") == 0 ) {
  196.         if ( (function != ENCODE_FRAMES) || (whichGOP != -1) ||
  197.          (frameStart != -1) ) {
  198.         Usage();
  199.         }
  200.  
  201.         function = COMBINE_FRAMES;
  202.         index++;
  203.     } else if ( strcmp(argv[index], "-child") == 0 ) {
  204.         if ( index+7 < argc-1 ) {
  205.         hostName = argv[index+1];
  206.         portNumber = atoi(argv[index+2]);
  207.         ioPortNumber = atoi(argv[index+3]);
  208.         combinePortNumber = atoi(argv[index+4]);
  209.         decodePortNumber = atoi(argv[index+5]);
  210.         machineNumber = atoi(argv[index+6]);
  211.         remoteIO = atoi(argv[index+7]);
  212.  
  213.         IOhostName = hostName;
  214.         } else {
  215.         Usage();
  216.         }
  217.  
  218.         childProcess = TRUE;
  219.         index += 8;
  220.     } else if ( strcmp(argv[index], "-io_server") == 0 ) {
  221.         if ( index+2 < argc-1 ) {
  222.         hostName = argv[index+1];
  223.         portNumber = atoi(argv[index+2]);
  224.         } else {
  225.         Usage();
  226.         }
  227.  
  228.         ioServer = TRUE;
  229.         index += 3;
  230.     } else if ( strcmp(argv[index], "-output_server") == 0 ) {
  231.         if ( index+3 < argc-1 ) {
  232.         hostName = argv[index+1];
  233.         portNumber = atoi(argv[index+2]);
  234.         outputFrames = atoi(argv[index+3]);
  235.         } else {
  236.         Usage();
  237.         }
  238.  
  239.         function = COMBINE_FRAMES;
  240.         outputServer = TRUE;
  241.         index += 4;
  242.     } else if ( strcmp(argv[index], "-decode_server") == 0 ) {
  243.         if ( index+3 < argc-1 ) {
  244.         hostName = argv[index+1];
  245.         portNumber = atoi(argv[index+2]);
  246.         outputFrames = atoi(argv[index+3]);
  247.         } else {
  248.         Usage();
  249.         }
  250.  
  251.         function = COMBINE_FRAMES;
  252.         decodeServer = TRUE;
  253.         index += 4;
  254.     } else if ( strcmp(argv[index], "-nice") == 0 ) {
  255.         niceProcesses = TRUE;
  256.         index++;
  257.     } else if ( strcmp(argv[index], "-max_machines") == 0 ) {
  258.         if ( index+1 < argc-1 ) {
  259.         maxMachines = atoi(argv[index+1]);
  260.         } else {
  261.         Usage();
  262.         }
  263.  
  264.         index += 2;
  265.     } else if ( strcmp(argv[index], "-quiet") == 0 ) {
  266.         if ( index+1 < argc-1 ) {
  267.         quietTime = atoi(argv[index+1]);
  268.         } else {
  269.         Usage();
  270.         }
  271.  
  272.         index += 2;
  273.     } else if ( strcmp(argv[index], "-no_frame_summary") == 0 ) {
  274.         if ( index < argc-1 ) {
  275.         frameSummary = FALSE;
  276.         } else {
  277.         Usage();
  278.         }
  279.  
  280.         index++;
  281.     } else if ( strcmp(argv[index], "-snr") == 0 ) {
  282.         printSNR = TRUE;
  283.         index++;
  284.     } else if ( strcmp(argv[index], "-debug_sockets") == 0 ) {
  285.         debugSockets = TRUE;
  286.     } else if ( strcmp(argv[index], "-debug_machines") == 0 ) {
  287.         debugMachines = TRUE;
  288.     } else {
  289.         Usage();
  290.     }
  291.     }
  292.  
  293.     if ( ! ReadParamFile(argv[argc-1], function) ) {
  294.     Usage();
  295.     }
  296.  
  297.     if ( printSNR || (referenceFrame == DECODED_FRAME) ) {
  298.     decodeRefFrames = TRUE;
  299.     }
  300.  
  301.     numMachines = min(numMachines, maxMachines);
  302.  
  303.     Frame_Init();
  304.  
  305. #ifdef BLEAH
  306.     time(&initTimeEnd);
  307.     fprintf(stdout, "INIT TIME:  %d seconds\n",
  308.         initTimeEnd-initTimeStart);
  309.     fflush(stdout);
  310. #endif
  311.  
  312.     if ( ioServer ) {
  313.     StartIOServer(numInputFiles, hostName, portNumber);
  314.     return 0;
  315.     } else if ( outputServer ) {
  316.     StartCombineServer(outputFrames, outputFileName, hostName, portNumber);
  317.     return 0;
  318.     } else if ( decodeServer ) {
  319.     StartDecodeServer(outputFrames, outputFileName, hostName, portNumber);
  320.     return 0;
  321.     }
  322.  
  323.     if ( (frameStart == -1) &&
  324.      ((numMachines == 0) || (function != ENCODE_FRAMES)) ) {
  325. /* @@@ Open binary mode for OS/2, Andy Key */
  326. #ifdef OS2
  327.     if ( (ofp = fopen(outputFileName, "wb")) == NULL ) {
  328. #else
  329.     if ( (ofp = fopen(outputFileName, "w")) == NULL ) {
  330. #endif
  331.         fprintf(stderr, "ERROR:  Could not open output file!\n");
  332.         exit(1);
  333.     }
  334.     }
  335.  
  336.     if ( ! childProcess ) {
  337.     fprintf(stdout, "Output File:  %s\n", outputFileName);
  338.     fprintf(stdout, "\n\n");
  339.     }
  340.  
  341.     if ( function == ENCODE_FRAMES ) {
  342.     if ( (numMachines == 0) || (frameStart != -1) ) {
  343.         time(&framesTimeStart);
  344.         totalTime = GenMPEGStream(whichGOP, frameStart, frameEnd,
  345.                       numInputFiles, ofp,
  346.                       outputFileName);
  347.         time(&framesTimeEnd);
  348.         if ( childProcess ) {
  349. #ifdef BLEAH
  350.         fprintf(stdout, "SCHEDULE:  MACHINE %d FRAMES %d-%d TIME %d-%d IOTIME %d\n",
  351.             machineNumber, frameStart, frameEnd,
  352.             framesTimeStart, framesTimeEnd,
  353.             IOtime);
  354. #endif
  355.         fprintf(stdout, "%s:  FRAMES %d-%d (%d seconds)\n",
  356.             getenv("HOST"), frameStart, frameEnd,
  357.             framesTimeEnd-framesTimeStart);
  358.         fflush(stdout);
  359.         }
  360.     } else {
  361.         /* check if parameter file has absolute path */
  362.         if ( (argv[argc-1][0] != '/') && (argv[argc-1][0] != '~') ) {
  363.         fprintf(stderr, "ERROR:  For parallel execution, please use absolute path for parameter file!\n");
  364.         exit(1);
  365.         } else {
  366.         StartMasterServer(numInputFiles, argv[argc-1], outputFileName);
  367.         }
  368.     }
  369.     } else if ( function == COMBINE_GOPS ) {
  370.     GOPStoMPEG(numInputFiles, outputFileName, ofp);
  371.     } else if ( function == COMBINE_FRAMES ) {
  372.     FramesToMPEG(numInputFiles, outputFileName, ofp, FALSE);
  373.     }
  374.  
  375.     if ( childProcess ) {
  376.     while ( NotifyMasterDone(hostName, portNumber, machineNumber,
  377.                  totalTime,
  378.                  &frameStart, &frameEnd) ) {
  379.         /* do more frames */
  380.         time(&framesTimeStart);
  381.         totalTime = GenMPEGStream(-1, frameStart, frameEnd,
  382.                       numInputFiles, NULL,
  383.                       outputFileName);
  384.         time(&framesTimeEnd);
  385. #ifdef BLEAH
  386.         fprintf(stdout, "SCHEDULE:  MACHINE %d FRAMES %d-%d TIME %d-%d IOTIME %d\n",
  387.             machineNumber, frameStart, frameEnd,
  388.             framesTimeStart, framesTimeEnd,
  389.             IOtime);
  390. #endif
  391.         fprintf(stdout, "%s:  FRAMES %d-%d (%d seconds)\n",
  392.             getenv("HOST"), frameStart, frameEnd,
  393.             framesTimeEnd-framesTimeStart);
  394.         fflush(stdout);
  395.     }
  396.     }
  397.  
  398.     Frame_Exit();
  399.  
  400.     return 0;    /* all is well */
  401. }
  402.  
  403.  
  404. /*=====================*
  405.  * INTERNAL PROCEDURES *
  406.  *=====================*/
  407.  
  408. /*===========================================================================*
  409.  *
  410.  * Usage
  411.  *
  412.  *    prints out usage for the program
  413.  *
  414.  * RETURNS:    nothing
  415.  *
  416.  * SIDE EFFECTS:    none
  417.  *
  418.  *===========================================================================*/
  419. static void
  420. Usage()
  421. {
  422.     fprintf(stderr, "Usage:  mpeg_encode [options] param_file\n");
  423.     fprintf(stderr, "Options:\n");
  424.     fprintf(stderr, "\t-stat stat_file:  append stats to stat_file\n");
  425.     fprintf(stderr, "\t-quiet n:  don't report remaining time for at least n seconds\n");
  426.     fprintf(stderr, "\t-no_frame_summary:  suppress frame summary lines\n");
  427.     fprintf(stderr, "\t-gop gop_num:  encode only the numbered GOP\n");
  428.     fprintf(stderr, "\t-combine_gops:  combine GOP files instead of encode\n");
  429.     fprintf(stderr, "\t-frames first_frame last_frame:  encode only the specified frames\n");
  430.     fprintf(stderr, "\t-combine_frames:  combine frame files instead of encode\n");
  431.     fprintf(stderr, "\t-nice:  run slave processes nicely\n");
  432.     fprintf(stderr, "\t-max_machines num_machines:  use at most num_machines machines\n");
  433.     fprintf(stderr, "\t-snr:  print signal-to-noise ratio\n");
  434.     exit(1);
  435.  
  436. /* extended usage (used by parallel code; shouldn't be called by user):
  437.     -child parallelHostName portNumber ioPortNumber combinePortNumber machineNumber remote
  438.     -io_server parallelHostName portNumber
  439.     
  440.     (remote = 1 if need to use ioPortNumber)
  441.  */
  442. }
  443.